<?php

namespace catchAdmin\apidoctor\service;

use app\common\model\ServicePackage as PackModel;
use think\Collection;
use catcher\exceptions\FailedException;
use app\common\model\Contract as ContractModel;

class MbqyService
{
    /**
     * 获取签约的服务包
     * @param int $user_id $user_id>0 医生端调用，否则是患者端调用
     * @param string $patientId
     * @param int $status 服务状态 1 进行中，2 全部
     * @return array
     */
    public function getSignedServicePack(int $user_id=0, string $patientId='', int $status=1): array
    {
        $packModel = (new PackModel());
        
        if ($user_id > 0) {
            $where[] = ['contract_doctor.doctor_id', '=', $user_id];
        }
        $where[] = ['patientId', '=', $patientId];

        //服务状态是进行中
        if ($status === 1) {
            $where[] = ['status', '=', 1001];
        }
        return (new ContractModel())->where($where)
                                    ->field('id,service_info,status')
                                    ->order('created_at','desc')
                                    ->select()
                                    ->each(function ($item, $key) use ($packModel) {
                                        $pack = $packModel->where('id',$item['service_info']['children'][0]['id'])->field('cycle')->find();
                                        $item['cycle'] = $pack['cycle'];
                                        return $item;
                                    })
                                    ->toArray();

    }

    /**
     * 获取签约的服务包详情
     * @param string $contract_id 签约id
     * @param string $pack_id 服务包id
     * @retrun array
     */
    public function getSignedServicePackDetails(string $contract_id, string $pack_id): array
    {
        $contract = (new ContractModel())->where('id', $contract_id)->find()->append(["status_text"]);//追加服务状态字段
        $pack = (new PackModel())->where('id', $contract['service_info']['children'][0]['id'])->find();

        //服务剩余天数
        $current_day = $this->calculateDays(date("Y-m-d"), $contract['service_end_date']);

        //默认剩余0天
        $remainder = 0;
        if ($current_day > 0) {
            $remainder = $current_day;
        }

        return [
            'contract_status' => [
                'status_text' => $contract['status_text'],
                'remainder' => $remainder
            ],
            'contract_details' => [
                'doctor_name' => $contract['contract_doctor'][0]['doctor_name'],
                'patient_name' => $contract['name'],
                'summary' => $pack['summary']
            ],
            'pack_info' => [
                'name' => $pack['name'],
                'cycle' => $pack['cycle'],
                'remainder' => $remainder
            ],
            'pack_content' => $contract['service_info']['children'][0]['children']
        ];
    }

    /**
     * 计算天数
     * @param string $start_date 开始日期
     * @param string $end_date 结束日期
     * @return int
     */
    private function calculateDays(string $start_date, string $end_date): int
    {
        $date1 = strtotime($start_date);
        $date2 = strtotime($end_date);

        $diff = $date2 - $date1;
        return floor($diff / (60 * 60 * 24));
    }
}